package data;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;

import model.Proveedor;

public class AdmPersistProveedor {

	private static AdmPersistProveedor instancia;
	
	private AdmPersistProveedor(){
		
	}
	
	public static AdmPersistProveedor getInstancia(){
		if(instancia==null)
			instancia=new AdmPersistProveedor();
		return instancia;
	}
	
	public void insert(Object o){
		
	}
	
	public Proveedor buscarProveedor(int cuit){
		try
		{
			Proveedor a = null;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select razSocial,direccion,telefono from seminario.dbo.Proveedores where cuit = ?");
			s.setInt(1, cuit);			
			ResultSet result = s.executeQuery();
			while (result.next())
			{
				String razSocial = result.getString(1);
				String direccion = result.getString(2);
				String telefono = result.getString(3);
				
				a = new Proveedor (cuit,razSocial,direccion,telefono);		    
				
			}
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return a;
			
		}
		catch (Exception e)
		{
			System.out.println();
		}
		return null;
	}
	
	public Proveedor buscarProveedor(String razonSocial){
		try
		{
			Proveedor a = null;
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement("select cuit,direccion,telefono from seminario.dbo.Proveedores where razonSocial like ?");
			s.setString(1, razonSocial);			
			ResultSet result = s.executeQuery();
			while (result.next())
			{
				long cuit = result.getLong(1);
				String direccion = result.getString(2);
				String telefono = result.getString(3);
				
				a = new Proveedor (cuit,razonSocial,direccion,telefono);		    
				
			}
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return a;
			
		}
		catch (Exception e)
		{
			System.out.println();
		}
		return null;
	}
	
	public Vector<Proveedor> selectAll(){
		try
		{
			Vector<Proveedor> rta = new	Vector<Proveedor>();
			Connection con = PoolConnection.getPoolConnection().getConnection();
			PreparedStatement s = con.prepareStatement(" select cuit,razonSocial,direccion,telefono from seminario.dbo.Proveedores");
			ResultSet result = s.executeQuery();
			while (result.next()){
				
				int cuit = result.getInt(1);
				String razonSocial = result.getString(2);
				String direccion = result.getString(3);
				String telefono = result.getString(4);
				
				Proveedor a = new Proveedor(cuit,razonSocial,direccion,telefono);
				rta.add(a);
				
			}
			PoolConnection.getPoolConnection().realeaseConnection(con);
			return rta;
			
			
		}catch (Exception e)
		{
			System.out.println("Error al leer la base de datos");
			System.out.println("Stack Trace al escribir la Base de datos: " + e.getStackTrace());
		}
		return null;
	}
}
